package com.xiaomi.service;

import com.xiaomi.annotation.SecureLogin;
import com.xiaomi.dao.UserDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
//import jakarta.annotation.Resource;//jdk11的Resource注解


@Service
public class UserServiceImpl implements UserService {

    // 定义属性
    @Autowired
    private UserDao userDao;

//    @Autowired
//    public UserServiceImpl(UserDao userDao) {
//        this.userDao = userDao;
//    }

//    @Autowired
//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }

//    public UserServiceImpl(@Autowired UserDao userDao) {
//        this.userDao = userDao;
//    }

    public void login() {
        // 调用dao层的login方法
        System.out.println("service层的login方法");
        userDao.login();
    }


    @SecureLogin(user_identify = "ADMIN")
    public void login_identify(String username) {
        // 模拟登录操作
        System.out.println("User " + username + " is trying to log in.");
        // 获取注解中的权限级别
        try {
            Method loginIdentify = getClass().getMethod("login_identify", String.class);
            SecureLogin annotation = loginIdentify.getAnnotation(SecureLogin.class);
            String identify = annotation.user_identify();
            if (checkLogging(identify, username)) {
                logLogin(username);
                System.out.println("User " + username + " logged in successfully.");
            } else {
                System.out.println("User " + username + " does not have the required permission.");
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    // 模拟校验服务
    public boolean checkLogging(String identify,String username) {
        // 这里可以实现具体的用户检查逻辑
        System.out.println("登录用户为: " + username + "可登录用户为: "+identify);
        return identify.equals(username);
    }
    // 模拟日志服务
    public void logLogin(String username) {
        System.out.println("User " + username + "trying logged in.");
    }
}

